/**
 * Copyright &copy; 2019-2022 <a href="https://gitee.com/rit/zx-pm">zx-pm</a> All rights reserved.
 */
package com.zx.pm.modules.zx.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zx.pm.common.config.Global;
import com.zx.pm.common.utils.StringUtils;
import com.zx.pm.common.web.BaseController;
import com.zx.pm.modules.zx.entity.Keyword;
import com.zx.pm.modules.zx.service.KeywordService;

/**
 * 采购目录Controller
 * @author forest
 * @version 2019-01-04
 */
@Controller
@RequestMapping(value = "${adminPath}/zx/keyword")
public class KeywordController extends BaseController {

	@Autowired
	private KeywordService keywordService;
	
	@ModelAttribute("keyword")
	public Keyword get(@RequestParam(required=false) String id) {
		if (StringUtils.isNotBlank(id)){
			return keywordService.get(id);
		}else{
			return new Keyword();
		}
	}

	@RequiresPermissions("zx:keyword:view")
	@RequestMapping(value = {""})
	public String index(Keyword keyword, Model model) {
		return "modules/zx/keywordIndex";
	}

	@RequiresPermissions("zx:keyword:view")
	@RequestMapping(value = {"list"})
	public String list(Keyword keyword, Model model) {
        model.addAttribute("list", keywordService.findList(keyword));
		return "modules/zx/keywordList";
	}
	
	@RequiresPermissions("zx:keyword:view")
	@RequestMapping(value = "form")
	public String form(Keyword keyword, Model model) {
		keyword.setParent(keywordService.get(keyword.getParent().getId()));
		// 自动获取排序号
		if (StringUtils.isBlank(keyword.getId()) && keyword.getParent() != null){
			int size = 0;
			List<Keyword> list = keywordService.findList();
			for (int i=0; i<list.size(); i++){
				Keyword e = list.get(i);
				if (e.getParent()!=null && e.getParent().getId()!=null
						&& e.getParent().getId().equals(keyword.getParent().getId())){
					size++;
				}
			}
			keyword.setCode(keyword.getParent().getCode() + StringUtils.leftPad(String.valueOf(size > 0 ? size+1 : 1), 3, "0"));
		}
		model.addAttribute("Keyword", keyword);
		return "modules/zx/keywordForm";
	}
	
	@RequiresPermissions("zx:keyword:edit")
	@RequestMapping(value = "save")
	public String save(Keyword keyword, Model model, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/zx/keyword/";
		}
		if (!beanValidator(model, keyword)){
			return form(keyword, model);
		}
		keywordService.save(keyword);
		addMessage(redirectAttributes, "保存'" + keyword.getName() + "'成功");
		String id = "0".equals(keyword.getParentId()) ? "" : keyword.getParentId();
		return "redirect:" + adminPath + "/zx/keyword/list?id=" + id + "&parentIds=" + keyword.getParentIds();
	}
	
	@RequiresPermissions("zx:keyword:edit")
	@RequestMapping(value = "delete")
	public String delete(Keyword keyword, RedirectAttributes redirectAttributes) {
		if(Global.isDemoMode()){
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + adminPath + "/zx/keyword/list";
		}
		keywordService.delete(keyword);
		addMessage(redirectAttributes, "删除成功");
		return "redirect:" + adminPath + "/zx/keyword/list?id="+keyword.getParentId()+"&parentIds="+keyword.getParentIds();
	}

	/**
	 * 获取机构JSON数据。
	 * @param extId 排除的ID
	 * @param response
	 * @return
	 */
	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Keyword> list = keywordService.findList();
		for (int i=0; i<list.size(); i++){
			Keyword e = list.get(i);
			if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("pIds", e.getParentIds());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
}
